#!/bin/bash
set -e

# 检测操作系统
detect_os() {
    if [[ "$OSTYPE" == "darwin"* ]]; then
        ostype="macOS"
        pkg_manager="brew"
    elif [[ -f /etc/os-release ]]; then
        . /etc/os-release
        case "$ID" in
            alpine) ostype="Alpine" ;;
            centos|rhel|fedora|kylin) 
                ostype="RHEL"
                [[ $(command -v dnf) ]] && pkg_manager="dnf" || pkg_manager="yum"
                ;;
            *) 
                ostype="Debian"
                pkg_manager="apt"
                ;;
        esac
    else
        echo "Unsupported OS"
        exit 1
    fi
}

# 安装依赖检测函数
check_and_install() {
    local pkg=$1
    if ! command -v $pkg &> /dev/null; then
        echo "$pkg is not installed. Installing..."
        case "$ostype" in
            macOS)
                echo "请使用Homebrew或Xcode命令行工具安装 $pkg"
                exit 1
                ;;
            Debian)
                sudo_cmd "apt update && apt install -y $pkg"
                ;;
            RHEL)
                sudo_cmd "$pkg_manager install -y $pkg"
                ;;
            Alpine)
                sudo_cmd "apk add --no-cache $pkg"
                ;;
            *)
                echo "Unsupported OS"
                exit 1
                ;;
        esac
    fi
}

# 带sudo权限检测的执行函数
sudo_cmd() {
    if sudo -v &> /dev/null; then
        sudo sh -c "$1"
    else
        echo "需要sudo权限执行：$1"
        exit 1
    fi
}

# 主流程
detect_os

# 安装基础依赖
case "$ostype" in
    RHEL)
        # 添加EPEL仓库（CentOS/麒麟OS）
        if [[ "$ID" == "centos" || "$ID" == "kylin" ]]; then
            sudo_cmd "$pkg_manager install -y epel-release"
        fi
        ;;
esac

check_and_install "git"
check_and_install "zsh"

# 安装oh-my-zsh
OMZ_DIR=$HOME/.oh-my-zsh
if [ ! -d "$OMZ_DIR" ]; then
    git clone --depth=1 https://gitee.com/huo15-study/ohmyzsh.git "$OMZ_DIR"
else
    echo "oh-my-zsh已存在，跳过克隆"
fi

# 初始化zsh配置文件
[ ! -f "$HOME/.zshrc" ] && cp "$OMZ_DIR/templates/zshrc.zsh-template" "$HOME/.zshrc"

# 安装插件
ZSH_CUSTOM="$OMZ_DIR/custom"
install_plugin() {
    local plugin=$1
    local repo=$2
    local dir="$ZSH_CUSTOM/plugins/$plugin"
    [ -d "$dir" ] || git clone --depth=1 "https://gitee.com/huo15-study/$repo.git" "$dir"
}

install_plugin "zsh-syntax-highlighting" "zsh-syntax-highlighting"
install_plugin "zsh-autosuggestions" "zsh-autosuggestions"
install_plugin "zsh-completions" "zsh-completions"

# 安装autojump
AUTOJUMP_DIR="$ZSH_CUSTOM/plugins/autojump"
if [ ! -d "$AUTOJUMP_DIR" ]; then
    git clone --depth=1 https://gitee.com/huo15-study/autojump.git "$AUTOJUMP_DIR"
    python3 $AUTOJUMP_DIR/install.py
else
    echo "autojump已存在，跳过安装"
fi

# 配置主题
P10K_DIR="$ZSH_CUSTOM/themes/powerlevel10k"
[ -d "$P10K_DIR" ] || git clone --depth=1 https://gitee.com/huo15-study/powerlevel10k.git "$P10K_DIR"

# 修改.zshrc配置
sed -i -e '/^plugins=/c\plugins=(git sudo z zsh-syntax-highlighting zsh-autosuggestions zsh-completions)' \
       -e '/^# zstyle/s/^# //' \
       -e '/^ZSH_THEME=/c\ZSH_THEME="powerlevel10k/powerlevel10k"' ~/.zshrc

# 设置默认shell
[[ $(basename $SHELL) != "zsh" ]] && chsh -s $(which zsh)

echo "安装完成！请重启终端使配置生效"